import {addClaimForex, deleteClaimForex, submitClaimForex, getClaimForexList, getUnrelatorderForexList, getRelatedorderForexList, addRelateForex, cancelRelateForex, confirmfinishForex} from '../../services/foreignExchange/chaim';
import {getForexList, getForexById,} from '../../services/foreignExchange/foreignExchangeManage';
import {parse} from 'qs';
import { message } from 'antd';

const dateFormat = 'YYYY-MM-DD';

export default {
    namespace: 'claim',
    state: {
        search: { // 查询条件
            payman: "",
            beginAmt: "",
            endAmt: "",
            payaccount: "",
            rectime: "",
            endTime: "",
            beginTime: ""
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            defaultWidth: 1000, // 缺省宽度800
            width: 1000, // 宽度
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '外汇认领', // 标题
            step : 0,
            unRelatedpage: {
                page: 1,
                size: 10,
                totalRow: 0,
                totalPage: 0
            },
            relatedpage: {
                page: 1,
                size: 10,
                totalRow: 0,
                totalPage: 0
            },
            exchangepage: {
                page: 1,
                size: 10,
                totalRow: 0,
                totalPage: 0
            },
            relatedlist: [],
            unRelatedlist: [],
            exchangelist: [],
        }
    },
    reducers: {

        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return {...state, search: action.payload.search};
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = {...state.editModel};
            if(payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if(payload.width != undefined) {
                newState.width = payload.width;
            }
            if(payload.title != undefined) {
                newState.title = payload.title;
            }
            if(payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }
            if(payload.step != undefined) {
                newState.step = payload.step;
            }

            if(payload.customsMode != undefined) {
                newState.customsMode = payload.customsMode;
            }
            if(payload.cencasementMode != undefined) {
                newState.cencasementMode = payload.cencasementMode;
            }
            if(payload.wordSendMode != undefined) {
                newState.wordSendMode = payload.wordSendMode;
            }

            if(payload.type != undefined) {
                newState.type = payload.type;
            }
            if(payload.form != undefined) {
                newState.form = payload.form;
            }
            if(payload.unRelatedpage != undefined) {
                newState.unRelatedpage = payload.unRelatedpage;
            }
            if(payload.unRelatedlist != undefined) {
                newState.unRelatedlist = payload.unRelatedlist;
            }
            if(payload.relatedpage != undefined) {
                newState.relatedpage = payload.relatedpage;
            }
            if(payload.relatedlist != undefined) {
                newState.relatedlist = payload.relatedlist;
            }
            if(payload.exchangelist != undefined) {
                newState.exchangelist = payload.exchangelist;
            }
            if(payload.exchangepage != undefined) {
                newState.exchangepage = payload.exchangepage;
            }

            return {...state, editModel: {...newState}};
        },


        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return {...state, list: payload.list, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return {...state, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setUnRelatedPage(state, { payload }) {
            let newState = {...state.editModel};
            newState.unRelatedpage = payload.unRelatedpage;
            return {...state, editModel: {...newState}};
        },

        setRelatedPage(state, { payload }) {
            let newState = {...state.editModel};
            newState.relatedpage = payload.relatedpage;
            return {...state, editModel: {...newState}};
        },

        setExchangePage(state, { payload }) {
            let newState = {...state.editModel};
            newState.exchangepage = payload.exchangepage;
            return {...state, editModel: {...newState}};
        }

    },
    effects: {
        /**
         * 获取外汇列表
         * @param call
         * @param put
         * @param select
         */
        *getForexList({}, { call, put, select }) {
            const search = yield select(state => state.claim.search);
            const page = yield select(state => state.claim.page);

            if (search.rectime != null && search.rectime != undefined) {
                if(search.rectime.length >=2 ){
                    if(search.rectime[0] != ''){
                        search.beginTime = search.rectime[0].format(dateFormat);
                    }
                    if(search.rectime[1] != ''){
                        search.endTime = search.rectime[1].format(dateFormat);
                    }

                }
            }

            const res = yield call(getForexList, {
                payman: search.payman,
                beginAmt: search.beginAmt,
                endAmt: search.endAmt,
                beginTime: search.beginTime,
                endTime: search.endTime,
                status: search.status,
                payaccount: search.payaccount,
                nameOrCode: search.nameOrCode,
                page
            });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page == undefined ? page : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取外汇详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getForexById({payload}, {call, put, select}) {
            const res = yield call(getForexById, {id: payload.id});
            let type = "setEditModel";

            if (payload.step == 2) {
                type = "getClaimForexList";

            }else if (payload.step == 3) {
                type = "getUnrelatorderForexList";
            }

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: type,
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: payload.title,
                        step: payload.step,
                        type: 'update',
                        forexId: content.id,
                        form: {
                            id: content.id,
                            recamount: content.recamount,
                            rectime: content.rectime,
                            payman: content.payman,
                            payaccount: content.payaccount,
                            settlement: content.settlement,
                            recaccount: content.recaccount,
                            memo: content.memo,
                            exchangetime: content.exchangetime,
                            bankexchangerate: content.bankexchangerate,
                            bankexchangeamount: content.bankexchangeamount,
                            cstexchangerate: content.cstexchangerate,
                            cstexchangeamount: content.cstexchangeamount,
                            recamountunit: content.recamountunit,
                            noclaimamount: content.noclaimamount,
                            type: payload.type,
                            norelateamount : content.norelateamount
                        }
                    }
                });

            } else {
                message.error(res.data.errMessage);
            }


        },

        /**
         * 获取分页认领
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getClaimForexList({payload}, { call, put, select }) {
            const page = yield select(state => state.claim.editModel.exchangepage);

            const res = yield call(getClaimForexList, {
                forexId: payload.form.id,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                let form  = payload.form;
                form.list = content.list;
                form.page = content.page == undefined ? payload.form.page : content.page;
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: payload.showStatus,
                        confirmLoading: payload.confirmLoading,
                        title: payload.title,
                        step: payload.step,
                        type: payload.type,
                        form: form,
                        exchangelist : content.list == undefined ? [] : content.list,
                        exchangepage : content.page == undefined ? page : content.page,

                    }
                });
            }
        },

        *setAmount({payload}, {call, put, select}) {
            let record = payload.record;
            let value = payload.value;
            record.amount = value;
        },

        /**
         * 查询关联订单
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getRelatedorderForexList({payload}, { call, put, select }) {
            const page = yield select(state => state.claim.editModel.relatedpage);

            const res = yield call(getRelatedorderForexList, {
                forexId: payload.forexId,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                let form  = payload.form;

                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: payload.showStatus,
                        confirmLoading: payload.confirmLoading,
                        title: payload.title,
                        step: payload.step,
                        type: payload.type,
                        form: form,
                        unRelatedpage: payload.unRelatedpage,
                        unRelatedlist: payload.unRelatedlist,
                        relatedpage : content.page == undefined ? page : content.page,
                        relatedlist : content.list == undefined ? [] : content.list,
            }
                });
            }else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 查询未关联订单
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getUnrelatorderForexList({payload}, { call, put, select }) {
            const page = yield select(state => state.claim.editModel.unRelatedpage);

            const res = yield call(getUnrelatorderForexList, {
                forexId: payload.forexId,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                let form  = payload.form;

                yield put({
                    type: 'getRelatedorderForexList',
                    payload: {
                        showStatus: payload.showStatus,
                        confirmLoading: payload.confirmLoading,
                        title: payload.title,
                        step: payload.step,
                        type: payload.type,
                        form: form,
                        forexId: payload.forexId,
                        unRelatedpage: content.page == undefined ? page : content.page,
                        unRelatedlist: content.list == undefined ? [] : content.list,

            }
                });
            }else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 认领外汇
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addClaimForex({ payload }, { call, put, select }) {
            const res = yield call(addClaimForex, payload);
            if (res.data.resultCode === "ok") {
                message.info("认领成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getForexList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 删除认领记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteClaimForex({ payload }, { call, put, select }) {
            const res = yield call(deleteClaimForex, {forexId : payload.forexId, claimId : payload.claimId });
            if (res.data.resultCode === "ok") {
                message.info("删除成功！");

                yield put({
                    type: "getForexById",
                    payload: {
                        id: payload.forexId,
                        step: 2

                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 提交认领记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *submitClaimForex({ payload }, { call, put, select }) {
            const res = yield call(submitClaimForex, {forexId : payload.forexId});
            if (res.data.resultCode === "ok") {

                let content = res.data.content

                if(payload.onSuccess)payload.onSuccess();
                message.info("提交认领记录成功！");

                yield put({
                    type: "setEditModel",
                    payload: {
                        showStatus: false
                    }
                });

                yield put({
                    type: 'getForexList'
                });
            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 关联订单
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addRelateForex({ payload }, { call, put, select }) {
            const res = yield call(addRelateForex, {
                forxid: payload.forxid,
                orderid: payload.orderid,
                relateamount: payload.relateamount,
                exportamount: payload.exportamount
            });
            if (res.data.resultCode === "ok") {
                let content = res.data.content
                message.info("关联成功！");
                yield put({
                    type: "getForexById",
                    payload: {
                        id: payload.forxid,
                        step: 3
                    }
                });
            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },


        /**
         * 取消关联成功
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *cancelRelateForex({ payload }, { call, put, select }) {

            const res = yield call(cancelRelateForex, {
                id: payload.id,
                forxid: payload.forxid,
                orderid: payload.orderid,
                relateamount: payload.relateamount
            });
            if (res.data.resultCode === "ok") {

                let content = res.data.content

                message.info("取消关联成功！");

                yield put({
                    type: "getForexById",
                    payload: {
                        id: payload.forxid,
                        step: 3

                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 确认外汇收齐
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *confirmfinishForex({ payload }, { call, put, select }) {
            console.info("confirmfinishForex");
            console.info(payload);

            const res = yield call(confirmfinishForex, {
                orderId: payload.orderid,
            });
            if (res.data.resultCode === "ok") {

                let content = res.data.content

                message.info(" 确认收汇成功！");

                yield put({
                    type: "getForexById",
                    payload: {
                        id: payload.forxid,
                        step: 3

                    }
                });

            } else {
                message.error(res.data.errMessage);
            }
        },


    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                dispatch({
                    type: 'getForexList'
                });
            });
        }
    }
};
